from datetime import datetime
from django.db import models


class GoodsCategory(models.Model):
    CATEGORY_TYPE = ((1, "一级类目"), (2, "二级类目"), (3, "三级类目"),)

    name = models.CharField('类别名', max_length=20)
    code = models.CharField('类别code', max_length=30)
    category_type = models.IntegerField('类别级别', choices=CATEGORY_TYPE)
    parent_category = models.ForeignKey('self', on_delete=models.CASCADE,
                                        null=True, blank=True, related_name='sub_cat')
    add_time = models.DateTimeField('添加时间', default=datetime.now)

    class Meta:
        verbose_name = '商品分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Goods(models.Model):
    category = models.ForeignKey(GoodsCategory, on_delete=models.CASCADE)
    name = models.CharField('商品名称', max_length=100)
    desc = models.CharField('商品描述', max_length=200)
    price = models.IntegerField('商品价格', default=0)
    sold_num = models.IntegerField('商品销售量', default=0)
    click_num = models.IntegerField('点击数', default=0)
    ship_free = models.BooleanField('是否承担运费', default=False)
    goods_front_img = models.ImageField('缩略图', upload_to='goods/front_image')
    add_time = models.DateTimeField('添加时间', default=datetime.now)

    class Meta:
        verbose_name = '商品'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class GoodsImage(models.Model):
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE)
    image = models.ImageField('商品轮播图', upload_to='goods/images')
    add_time = models.DateTimeField('添加时间', default=datetime.now)

    class Meta:
        verbose_name = '商品轮播'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goods.name
